package com.SmartCare.service.impl.health;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.SmartCare.common.Result;
import com.SmartCare.common.vo.health.BodyHeartRateVo;
import com.SmartCare.mapper.health.BodyHeartRateMapper;
import com.SmartCare.pojo.health.BodyHeartRate;
import com.SmartCare.service.health.BodyHeartRateService;
import com.SmartCare.utils.DateTimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;


@Service
public class BodyHeartRateServiceImpl extends ServiceImpl<BodyHeartRateMapper,BodyHeartRate> implements BodyHeartRateService {
    /**
     * 心率记录查询
     */
    @Override
    public Result getWeekInfo(Long elderlyId, String startTime, String endTime) {
        //构造查询条件
        LambdaQueryWrapper<BodyHeartRate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BodyHeartRate::getElderlyId,elderlyId);
        wrapper.between(BodyHeartRate::getCreatedTime,startTime,endTime);
        wrapper.orderByAsc(BodyHeartRate::getCreatedTime);
        //查询记录
        List<BodyHeartRate> list = list(wrapper);
        if (list.isEmpty()) {
            return Result.error("无记录");
        }
        //封装返回的信息
        List<BodyHeartRateVo> res= new ArrayList<>();
        list.forEach((item)->{
            BodyHeartRateVo vo = new BodyHeartRateVo();
            BeanUtils.copyProperties(item,vo,"createdTime");
            vo.setCreatedTime(DateTimeUtils.formatDateTime(item.getCreatedTime()));
            res.add(vo);
        });
        return Result.success(res);
    }

    /**
     * 心率记录添加
     */
    @Override
    public Result heartRateAdd(Long elderlyId, Float heartRate) {
        if (elderlyId==null) {
            return Result.error("未知错误");
        }
        if(!(heartRate>=0)){
            return Result.error("数据输入错误");
        }
        BodyHeartRate one = BodyHeartRate.builder()
                .elderlyId(elderlyId)
                .heartRate(heartRate)
                .build();
        boolean res = save(one);
        if (!res){
            return Result.error("添加失败");
        }
        return Result.success("添加完成");
    }
}
